package com.example.controller;


import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.pojo.Shift;
import com.example.service.ShiftService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 班次管理控制器
 */
@Slf4j
@RequestMapping("/shifts")
@RestController
public class ShiftController {

    @Autowired
    private ShiftService shiftService;

    /**
     * 分页查询班次 -GET http://localhost:8881/shifts?page=1&pageSize=10
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询班次，当前页码为：{}，每页记录数：{}",page,pageSize);
        PageResult  pageResult = shiftService.page(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 删除指定ID的班次 -DELETE http://localhost:8881/shifts?ID=1
     */
    @DeleteMapping
    public Result deleteById(Integer ID){
        log.info("删除指定ID的班次：{}",ID);
        shiftService.deleteById(ID);
        return Result.success();
    }

    /**
     * 保存新的班次 -POST http://localhost:8881/shifts
     */
    @PostMapping
    public Result save(@RequestBody Shift shift){
        log.info("保存新的班次：{}",shift);
        shiftService.save(shift);
        return Result.success();
    }

    /**
     * 修改指定ID的班次 -PUT http://localhost:8881/shifts
     */
    @PutMapping
    public Result update(@RequestBody Shift shift){
        log.info("修改指定ID的班次：{}",shift);
        shiftService.update(shift);
        return Result.success();
    }

}
